//
//  HelpMeBagApplySecondStepVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/9.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "HelpMeBagApplySecondStepVC.h"
#import "HelpMeBagApplyFinishVC.h"
#import "PickViewSelect.h"

@interface HelpMeBagApplySecondStepVC ()<pickViewStrDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIView *view_topFirstStepBG;

@property (nonatomic,strong) UIView *view_bottomFirstStepBG;

@property (nonatomic,strong) UITextField *textField_age;

@property(nonatomic,strong) PickViewSelect *pickView;

@end

@implementation HelpMeBagApplySecondStepVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帮我贷";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    [self initWithHelpMeBagApplySecondStepVCSubviews];
    
    [self initWithHelpMeBagApplySecondStepBottom];
    
    //button
    UIButton *button_nextStep = [[UIButton alloc] initWithFrame:CGRectMake(48*PROPORTION_WIDTH,75*PROPORTION_WIDTH+self.view_bottomFirstStepBG.bottom,kScreenWidth - 48*PROPORTION_WIDTH*2, 93*PROPORTION_WIDTH)];
    [self.view addSubview:button_nextStep];
    
    button_nextStep.backgroundColor = COLOR_BUTTON;
    
    [button_nextStep setTitle:@"提交申请" forState:UIControlStateNormal];
    [button_nextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_nextStep.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18*2]];
    button_nextStep.layer.cornerRadius = 3.0;
    button_nextStep.clipsToBounds = YES;
    
    [button_nextStep addTarget:self action:@selector(ApplayNowSubmitAction) forControlEvents:UIControlEventTouchUpInside];
    
    //注册键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark --- 创建视图界面
- (void) initWithHelpMeBagApplySecondStepVCSubviews
{
    self.view_topFirstStepBG = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 245*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_topFirstStepBG];
    self.view_topFirstStepBG.backgroundColor = [UIColor whiteColor];
    
    //展位的背景图
    UIImageView *imageView_fullImage = [[UIImageView alloc] initWithFrame:CGRectMake(73*PROPORTION_WIDTH,48*PROPORTION_WIDTH, kScreenWidth-73*PROPORTION_WIDTH*2, 133*PROPORTION_WIDTH)];
    [self.view_topFirstStepBG addSubview:imageView_fullImage];
    imageView_fullImage.image = [UIImage imageNamed:@"step2"];
    
    //横线 second
    UIView *view_line  = [[UIView alloc] initWithFrame:CGRectMake(0,220*PROPORTION_WIDTH,kScreenWidth,25.0*PROPORTION_WIDTH)];
    [self.view_topFirstStepBG addSubview:view_line];
    view_line.backgroundColor = COLOR_EVERYVIEWBG;
}

#pragma mark --- 创建视图界面 底部视图
- (void) initWithHelpMeBagApplySecondStepBottom
{
    //①②③④⑤⑥⑦⑧⑨⑩
    self.view_bottomFirstStepBG = [[UIView alloc] initWithFrame:CGRectMake(0, self.view_topFirstStepBG.bottom, kScreenWidth, 531*PROPORTION_WIDTH)];
    [self.view addSubview:self.view_bottomFirstStepBG];
    self.view_bottomFirstStepBG.backgroundColor = [UIColor whiteColor];
    NSArray *array_title = @[@"①  职业身份",@"②  是否有本地公积金",@"③  是否有本地社保",@"④  名下房产类型",@"⑤  名下是否有车",@"⑥  您的信用情况",@"⑦  年龄"];
    //左侧title
    for (int i=0; i<7; i++) {
        UILabel *label_title = [[UILabel alloc] initWithFrame:CGRectMake(72*PROPORTION_WIDTH, 37*PROPORTION_WIDTH+(32+40)*PROPORTION_WIDTH*i, 245*PROPORTION_WIDTH, 40*PROPORTION_WIDTH)];
        [self.view_bottomFirstStepBG addSubview:label_title];
        label_title.text = array_title[i];
        label_title.font = [UIFont systemFontOfSize:Font_SIZE_24];
        label_title.textColor = [BaseViewController colorWithHexString:@"#4d4d4d"];
        
        if (i!=6) {
            //button
            UIButton * button_choose = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-275*PROPORTION_WIDTH, label_title.y, 192*PROPORTION_WIDTH, label_title.height)];
            [self.view_bottomFirstStepBG addSubview:button_choose];
            button_choose.layer.borderWidth = 1.0;
            button_choose.layer.borderColor = [BaseViewController colorWithHexString:@"#c4c4c4"].CGColor;
            button_choose.tag = 20+i;
            [button_choose addTarget:self action:@selector(choseDifferentInfo:) forControlEvents:UIControlEventTouchUpInside];
            
            //imageView
            UIImageView *imageView_money = [[UIImageView alloc] initWithFrame:CGRectMake(button_choose.width-button_choose.height, 0,button_choose.height, button_choose.height)];
            [button_choose addSubview:imageView_money];
            imageView_money.image = [UIImage imageNamed:@"arrowdown"];
        }
        if (i==6) {
            self.textField_age = [[UITextField alloc] initWithFrame:CGRectMake(kScreenWidth-275*PROPORTION_WIDTH, label_title.y, 192*PROPORTION_WIDTH-label_title.height, label_title.height)];
            [self.view_bottomFirstStepBG addSubview:self.textField_age];
            self.textField_age.layer.borderWidth = 1.0;
            self.textField_age.layer.borderColor = [BaseViewController colorWithHexString:@"#c4c4c4"].CGColor;
            self.textField_age.keyboardType = UIKeyboardTypeNumberPad;
            self.textField_age.font = [UIFont systemFontOfSize:Font_SIZE_24];
        
            self.textField_age.delegate = self;
            [self.textField_age addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            
            //age
            UILabel *label_age = [[UILabel alloc] initWithFrame:CGRectMake(self.textField_age.right,self.textField_age.y, label_title.height, label_title.height)];
            [self.view_bottomFirstStepBG addSubview:label_age];
            label_age.text = @"岁";
            label_age.font = [UIFont systemFontOfSize:Font_SIZE_24];
            label_age.textColor = [BaseViewController colorWithHexString:@"#4d4d4d"];
            label_age.textAlignment = NSTextAlignmentCenter;
        }
    }
}

#pragma mark -- 选择不同的 条件
- (void) choseDifferentInfo:(UIButton *)sender
{
    NSArray *array_allData = @[@[@"企业主",@"个体户",@"上班族",@"学生",@"无固定职业"],@[@"是",@"否"],@[@"是",@"否"],@[@"无房产",@"商品房",@"商铺",@"办公楼",@"厂房",@"经济房",@"自建房"],@[@"无车",@"有车",@"有车但已抵押"],@[@"无信用卡或贷款",@"有信用卡或贷款且及时还款",@"一年内逾期少于3次且少于90天",@"一年内逾期超过3次或超过90天"]];
    
    _pickView =[[PickViewSelect alloc]initWithDataArr:array_allData[sender.tag-20] andRowIndex:(int)sender.tag];
    _pickView.delegate = self;
    [self.view addSubview:_pickView];
    
}

/**
 * 限制 年龄输入的长度
 */
- (void) textFieldDidChange:(UITextField *)textField
{
    if (textField==self.textField_age) {
        
        [self.mutableDic_pathBagInfo setObject:self.textField_age.text forKey:@"age"];
        
        if (self.textField_age.text.length>2) {
            [self.textField_age.text substringToIndex:3];
            textField.text = [textField.text substringToIndex:3];
        }
    }
}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view_bottomFirstStepBG endEditing:YES];
    [self.view endEditing:YES];
    [self.view_topFirstStepBG endEditing:YES];
}

#pragma mark -- picker delegate
- (void) pickViewdelegateWith:(NSString *)messStr andRow:(int)rowIndex andSelectecRow:(int)selectedRow
{
    UIButton *button_temp = (UIButton *)[self.view_bottomFirstStepBG viewWithTag:rowIndex];
    [button_temp setTitle:messStr forState:UIControlStateNormal];
    
    [button_temp setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    
    [button_temp setTitleColor:[BaseViewController colorWithHexString:@"#4d4d4d"] forState:UIControlStateNormal];
    [button_temp.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_24]];
    
    if (rowIndex==20) {
        int zhiyeshenf = 0;
        if ([messStr isEqualToString:@"企业主"]) {
            zhiyeshenf = 1;
        }
        if ([messStr isEqualToString:@"个体户主"]) {
            zhiyeshenf = 2;
        }
        if ([messStr isEqualToString:@"上班族"]) {
            zhiyeshenf = 3;
        }
        if ([messStr isEqualToString:@"4"]) {
            zhiyeshenf = 4;
        }
        if ([messStr isEqualToString:@"无固定职业"]) {
            zhiyeshenf = 5;
        }
        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:zhiyeshenf] forKey:@"zhiyeshenf"];
    }
    
    if (rowIndex==21) {
        int isyougjj = 3;
        if ([messStr isEqualToString:@"否"]) {
            isyougjj = 0;
        }
        if ([messStr isEqualToString:@"是"]) {
            isyougjj = 1;
        }
        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:isyougjj] forKey:@"isyougjj"];
    }
    
    if (rowIndex==22) {
        int isshebao = 3;
        if ([messStr isEqualToString:@"否"]) {
            isshebao = 0;
        }
        if ([messStr isEqualToString:@"是"]) {
            isshebao = 1;
        }
        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:isshebao] forKey:@"isshebao"];
    }
    
    if (rowIndex==23) {
        int hometype = 0;
        if ([messStr isEqualToString:@"无房产"]) {
            hometype = 1;
        }
        if ([messStr isEqualToString:@"商品房"]) {
            hometype = 2;
        }
        if ([messStr isEqualToString:@"商铺"]) {
            hometype = 3;
        }
        if ([messStr isEqualToString:@"办公楼"]) {
            hometype = 4;
        }
        if ([messStr isEqualToString:@"厂房"]) {
            hometype = 5;
        }
        if ([messStr isEqualToString:@"经济房"]) {
            hometype = 6;
        }
        if ([messStr isEqualToString:@"自建房"]) {
            hometype = 7;
        }
        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:hometype] forKey:@"hometype"];
    }
    
    if (rowIndex==24) {
        int iscar = 0;
        if ([messStr isEqualToString:@"无车"]) {
            iscar = 1;
        }
        if ([messStr isEqualToString:@"有车"]) {
            iscar = 2;
        }
        if ([messStr isEqualToString:@"有车但已抵押"]) {
            iscar = 3;
        }
        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:iscar] forKey:@"iscar"];
    }
    
    if (rowIndex==25) {
        int cardqingk = 0;
        if ([messStr isEqualToString:@"无信用卡或贷款"]) {
            cardqingk = 1;
        }
        if ([messStr isEqualToString:@"有信用卡或贷款且及时还款"]) {
            cardqingk = 2;
        }
        if ([messStr isEqualToString:@"一年内逾期少于3次且少于90天"]) {
            cardqingk = 3;
        }
        if ([messStr isEqualToString:@"一年内逾期超过3次或超过90天"]) {
            cardqingk = 4;
        }

        [self.mutableDic_pathBagInfo setObject:[NSNumber numberWithInt:cardqingk] forKey:@"cardqingk"];
    }
}

#pragma mark --- 提交申请
- (void) ApplayNowSubmitAction
{
    if (self.mutableDic_pathBagInfo.count==14) {
        HelpMeBagApplyFinishVC *finishView = [[HelpMeBagApplyFinishVC alloc] init];
        finishView.mutableDic_pathBagInfo = self.mutableDic_pathBagInfo;
        
        [self.navigationController pushViewController:finishView animated:YES];
    }
    else
    {
        [UIView showAlertMsg:@"请完善信息"];
    }
}

#pragma mark keyboardNotification

- (void)keyboardWillShow:(NSNotification*)notification{
    CGRect _keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //如果self在键盘之下 才做偏移
    if ([self convertYToWindow:CGRectGetMaxY(self.view_bottomFirstStepBG.frame)]>=_keyboardRect.origin.y)
    {
        //没有偏移 就说明键盘没出来，使用动画
        if (self.view.frame.origin.y== self.view_bottomFirstStepBG.frame.origin.y) {
            
            [UIView animateWithDuration:0.3 animations:^{
                self.view.frame = CGRectMake(0, -_keyboardRect.size.height+[self getHeighOfWindow]-CGRectGetMaxY(self.view.frame), kScreenWidth, kScreenHeight);
            }];
        }
        else
        {
            self.view.frame = CGRectMake(0, -105, kScreenWidth, kScreenHeight);
        }
    }
}

- (void)keyboardWillHide:(NSNotification*)notification{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    }];
}

#pragma  mark ConvertPoint
//将坐标点y 在window和superview转化  方便和键盘的坐标比对
-(float)convertYFromWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [appDelegate.window convertPoint:CGPointMake(0, Y) toView:self.view];
    return o.y;
    
}
-(float)convertYToWindow:(float)Y
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CGPoint o = [self.view convertPoint:CGPointMake(0, Y) toView:appDelegate.window];
    return o.y;
    
}
-(float)getHeighOfWindow
{
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return appDelegate.window.frame.size.height;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end
